var api = require('../../utils/api.js');
var app = getApp();
import jiuaiDebounce from '../../utils/jiuai-debounce.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList:[]
  },

  //新增收货地址页面
  addAddress(){
    wx.navigateTo({
      url: '/pages/addAddress/addAddress',
    })
  },

  //跳转编辑地址
  editAdress(e){
    let item = e.currentTarget.dataset.item;
    console.log(item)
    wx.navigateTo({
      url: '/pages/addAddress/addAddress?isedit=1&itemArr='+JSON.stringify(item),
    })
  },
//获取收货地址列表
getAddressList(){
  api.fetchApi('/Shipping_address/index', { token: wx.getStorageSync('token') }).then(res=>{
    let addressList=res.list;
    this.setData({
      addressList
    })
  })
},

//选择收货地址
  selectAds(e){
    if (this.data.pageUrl =='FillOrder'){
      let receiveAds = e.currentTarget.dataset.item;
      console.log(receiveAds)
      wx.setStorageSync('receiveAds', receiveAds);
      let pages = getCurrentPages();    //获取当前页面信息栈
      let prevPage = pages[pages.length - 2]     //获取上一个页面信息栈
      prevPage.getGoodsList();
     // setTimeout(() => {
        wx.navigateBack({

        })
     // }, 500)
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let pageUrl=options.page || ''
    console.log(pageUrl);
    this.setData({
      pageUrl
    })
    this.getAddressList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.data.addressList.length==0){
      wx.removeStorageSync('receiveAds')
      let pages = getCurrentPages();    //获取当前页面信息栈
      let prevPage = pages[pages.length - 2]     //获取上一个页面信息栈
      if (this.data.pageUrl =='FillOrder') prevPage.getGoodsList();
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})